package com.fancy.learncenter.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.MsgXTadapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.MsgCommentDataBean;
import com.fancy.learncenter.bean.MsgSysDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MsgXTActivity extends BaseActivity {

    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.prompt_img)
    ImageView promptImg;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_system, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("系统消息");

        adapter = new MsgXTadapter(this, new ArrayList<MsgSysDataBean.ListBean>());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        recycleView.setAdapter(adapter);
        adapter.setCustomLoadMoreView(new CustomFootView(this));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                getData(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getData(false);
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });

        getData(true);

//        msgComment();
//
//        commentReply();
    }

    MsgXTadapter adapter;
    int page = 0;

    private void getData(final boolean isRefresh) {
        Map map = new HashMap();

        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        map.put("page", page + "");
        HttpMehtod.getInstance().msgSystem(map, new IdeaObserver<BaseDataBean<MsgSysDataBean>>() {
            @Override
            public void onSuccess(BaseDataBean<MsgSysDataBean> msgSysDataBeanBaseDataBean) {

//                if (msgSysDataBeanBaseDataBean.getData() != null && msgSysDataBeanBaseDataBean.getData().getBanner_arr() != null) {
//                    adapter.notifyDataSetChanged((ArrayList<MsgSysDataBean.BannerArrBean>) msgSysDataBeanBaseDataBean.getData().getBanner_arr());
//                    promptImg.setVisibility(View.GONE);
//                } else {
//                    promptImg.setVisibility(View.VISIBLE);
//                    adapter.notifyDataSetChanged(new ArrayList<MsgSysDataBean.BannerArrBean>());
//                }


                if (isRefresh) {
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);

                    if (msgSysDataBeanBaseDataBean.getData() == null || msgSysDataBeanBaseDataBean.getData().getList() == null || msgSysDataBeanBaseDataBean.getData().getList().size() == 0) {
                        adapter.notifyDataSetChanged(new ArrayList<MsgSysDataBean.ListBean>());
                        promptImg.setVisibility(View.VISIBLE);
                        return;
                    } else {
                        promptImg.setVisibility(View.GONE);
                        adapter.notifyDataSetChanged((ArrayList<MsgSysDataBean.ListBean>) msgSysDataBeanBaseDataBean.getData().getList());
                    }


                } else {
                    if (msgSysDataBeanBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (msgSysDataBeanBaseDataBean.getData() != null && msgSysDataBeanBaseDataBean.getData().getBanner_arr() != null && msgSysDataBeanBaseDataBean.getData().getBanner_arr().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (msgSysDataBeanBaseDataBean.getData() == null) {
                        return;
                    }
                    adapter.addAll(msgSysDataBeanBaseDataBean.getData().getList());
                }
            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }
        });
    }

}
